package com.chaowen.yixin;

import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chaowen.model.UserInfo;
import com.chaowen.service.TaskContext;
import com.chaowen.util.PreferenceHelper;
import com.chaowen.util.WifiHelper;

public class Fragment_login extends Fragment_super{
	//控件
	public static Button loginBtn;
	public static EditText username;
	public static EditText password;
	public static TextView rememberTextView;
	//属性
	private Boolean isRemembered;
	private Handler handler;
	private PreferenceHelper helper;
	private String userNameString;
	private String passwordString;
	private Context context;
	private WifiHelper wifiHelper;
	
	public Fragment_login(Handler handler) {
		this.handler = handler;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_login, container, false);
		initView(view);
		context = getActivity();
		
		wifiHelper = new WifiHelper(context);
		if (!(wifiHelper.isConnected())) {
			wifiHelper.openWifiSetting();
		}
		
		helper = new PreferenceHelper(context);
		HashMap<String,Object> dataMap = helper.getPreferences();
//		UserInfo.isLogin = ((Boolean)dataMap.get("isLogin")).booleanValue();
		isRemembered = ((Boolean)dataMap.get("isRemembered")).booleanValue();
		userNameString = (String)dataMap.get("username");
		passwordString = (String)dataMap.get("password");
		UserInfo.userName = userNameString;
//		System.out.println("是否处于登陆状态： "+UserInfo.isLogin);
		if (UserInfo.isLogin) {
			loginBtn.setText("退出");
//			loginBtn.setEnabled(false);
			loginBtn.setBackgroundResource(R.drawable.press_button);
			username.setText(userNameString);
			password.setText(passwordString);
			username.setEnabled(false);
			password.setEnabled(false);
			rememberTextView.setEnabled(false);
//			if (isRemembered) {
//				rememberTextView.setEnabled(true);
//				rememberTextView.setText("记住密码?");
//			} else {
//				rememberCheck.setChecked(false);
//			}
		} else {
			rememberTextView.setEnabled(true);
			if (isRemembered) {
				username.setText(userNameString);
				password.setText(passwordString);
			} 
//			else {
//				rememberCheck.setChecked(false);
//			}
		}
		return view;
	}
	
	/**
	 * 初始化View对象
	 */
	private void initView(View view) {
		username = (EditText)view.findViewById(R.id.usernameText);
		password = (EditText)view.findViewById(R.id.passwordText);
		System.out.println("获取到了信息：  "+username.getText().toString()+"   "+password.getText().toString());
		loginBtn = (Button)view.findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(this);
		rememberTextView = (TextView)view.findViewById(R.id.rememberTextView);
		rememberTextView.setOnClickListener(this);
//		rememberCheck = (CheckBox)view.findViewById(R.id.rememberCheck);
//		rememberCheck
//		.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//			public void onCheckedChanged(
//					CompoundButton paramCompoundButton,
//					boolean paramBoolean) {
//				Fragment_login.this.isRemembered = paramBoolean;
//			}
//		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(!isRemembered&&!UserInfo.isLogin) {
			userNameString = "";
			passwordString = "";
		}
		helper.save(userNameString,passwordString,isRemembered);
		System.out.println("保存数据到了csunet"+passwordString+"     "+isRemembered+"　　　　"+UserInfo.isLogin);
	}
	@Override
	public void onClick(View v) {
		if(v == loginBtn) {
			if (UserInfo.isLogin) {
				logout();
			} else {
				login();
			}
		}else if(v == rememberTextView) {
			rememberPassword();
		}
	}
	
	/**
	 * 选择是否记住密码
	 */
	private void rememberPassword() {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(context);
		localBuilder
//				.setTitle("记住密码")
				.setMessage("\t\t\t\t\t\t\t\t\t 是否记住密码")
				.setNegativeButton("不记住", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {
						isRemembered = false;
					}
				})
				.setPositiveButton("记住", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {
						isRemembered = true;
					}
				}).create();
		localBuilder.show();
	}
	
	/**
	 * 登陆操作
	 */
	private void login() {
		userNameString = username.getText().toString().trim();
		passwordString = password.getText().toString().trim();
		if ((TextUtils.isEmpty(userNameString)) || (TextUtils.isEmpty(passwordString))) {
			handler.sendEmptyMessage(0);
		} else {
			UserInfo.userName = userNameString;
			UserInfo.password = passwordString;
			loginBtn.setText("正在登录中...");
//			Task task = TaskFactory.createTask("login");//生产一个Task
//			task.setHandler(handler);//设置handler
//			task.execute();//开启登陆线程
//			// 参数有：用户名、密码
//			new LoginTask(this.handler)
//					.execute();
			new TaskContext("LoginTask").doTask(handler);//策略模式
		}
	}
	
	/**
	 * 登出操作
	 */
	private void logout() {
		if (!(this.wifiHelper.isConnected())) {
			Toast.makeText(context, "未检测到WIFI连接", 0).show();
		} else {
			// 参数IP地址
			new TaskContext("LogOutTask").doTask(handler);//策略模式
		}
	}
	
}
